package com.opennews.openplatform

import groovy.transform.CompileStatic
import org.springframework.http.HttpStatus

@CompileStatic
class BankCardController extends BaseController {
    static allowedMethods = [
        add: "POST",
        update: "POST",
        delete: "POST",
        batchDelete: "POST"
    ]

    BankCardService bankCardService

    // region: Get actions.
    def index() {
        render("BankCardController")
    }

    def queryAllWithBasicInfoByAccountGroup() {
        def result = bankCardService.queryAllWithBasicInfoByAccountGroup(getClaims().accountGroupId)

        sendDataResponse(result)
    }

    def queryBasicInfoByAccountGroup() {
        def pageSize = params.int("pageSize")
        def pageIndex = params.int("pageIndex")
        def accountGroupId = getClaims().accountGroupId
        def result = bankCardService.queryBasicInfoByAccountGroup(accountGroupId, pageSize, pageIndex)

        sendDataResponse(result)
    }

    def queryBankCardInfo() {
        def id = params.id.toString()
        def accountGroupId = getClaims().accountGroupId
        def result = bankCardService.queryBankCardInfo(accountGroupId, id)

        sendDataResponse(result)
    }
    // endregion

    // region: Post actions.
    def add() {
        def requestBodyData = getRequestBodyData()
        def accountGroupId = getClaims().accountGroupId
        bankCardService.add(accountGroupId, requestBodyData)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }

    def update() {
        def requestBodyData = getRequestBodyData()
        def accountGroupId = getClaims().accountGroupId
        bankCardService.update(accountGroupId, requestBodyData)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }

    def delete() {
        def requestBodyData = getRequestBodyData()
        def id = requestBodyData.id.toString()
        def accountGroupId = getClaims().accountGroupId
        def result = bankCardService.delete(accountGroupId, id)

        sendDataResponse(result)
    }

    def batchDelete() {
        def requestBodyData = getRequestBodyData()
        def ids = requestBodyData.ids as List
        def accountGroupId = getClaims().accountGroupId
        bankCardService.delete(accountGroupId, ids)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }
    // endregion
}